package cn.itcast.service.impl;

import cn.itcast.mapper.UserMapper;
import cn.itcast.pojo.User;
import cn.itcast.service.UserService;
import cn.itcast.exception.ExceptionCast;
import cn.itcast.response.ResponseResult;
import cn.itcast.response.StatusCode;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    /**
     * 添加用户
     * @param user
     * @return
     */
    @Override
    public User add(User user) {
        if (user.getUserName()==null){
            ExceptionCast.cast(ResponseResult.errorResult(StatusCode.APUSER_USERNAMEISNULL_ERROR));
        }
           userMapper.add(user);
        return user;
    }

    /**
     * 分页查询
     *
     * @param user
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Page<User> findPage(User user, Integer page, Integer pageSize) {

        PageHelper.startPage(page,pageSize);
        Page<User>pageList=userMapper.findPage(user);
        return pageList;
    }


}
